<template>
  <div class="Number">
    <van-button  color="#efefef" size="small" @click="minus()" :disabled="value<=1"><van-icon name="minus" color="#ff6034" /></van-button>
    <van-field :value="value" @change="handleChange" type="number" center input-align="center"/>
    <van-button  color="#efefef" size="small" @click="plus()"><van-icon name="plus" color="#ee0a24" /></van-button>
  </div>
</template>

<script>
export default {
  name: 'NumberItem',
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  data () {
    return {
    }
  },
  methods: {
    plus () {
      this.$emit('input', this.value + 1)
    },
    minus () {
      if (this.value <= 1) return
      this.$emit('input', this.value - 1)
    },
    handleChange (e) {
      console.log(e.target.value)
      const num = +e.target.value // 转数字处理 (1) 数字 (2) NaN
      if (isNaN(num) || num < 1) {
        e.target.value = this.value
        return
      }
      this.$emit('input', num)
    }
  }
}
</script>

<style lang="less" scoped>
.Number {
  width: 120px;
  display: flex;
  .van-field {
    width: 50px;
    height: 30px;
    outline: none;
    border: none;
    margin: 0 5px;
    background-color: #efefef;
  }
}
</style>
